package com.shuo.dsa.niuke;

import java.util.Stack;

/**
 * 定义栈的数据结构，请在该类型中实现一个能够得到栈中所含最小元素的min函数
 * 思路: 借助2个栈, 一个数据栈一个min栈
 * eg: data: 5, 4, 3, 8, 10, 11, 12, 1
 *      min: 5, 4, 3，3, 3,  3,  3, 1
 */
public class StackWithMin {
    private Stack<Integer> stack = new Stack<>();
    private Stack<Integer> minStack = new Stack<>();

    public void push(int node) {
        stack.push(node);
        if (minStack.isEmpty()) {
            minStack.push(node);
        } else {
            int min = minStack.peek();
            if (min > node) minStack.push(node);
            else minStack.push(min);
        }
    }

    public void pop() {
        stack.pop();
        minStack.pop();
    }

    public int top() {
        return stack.peek();
    }

    public int min() {
        return minStack.peek();
    }
}
